package com.iotproject.studyservice.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyReading;
import com.iotproject.studyservice.entity.vo.admin.BookQuery;
import com.iotproject.studyservice.service.StudyReadingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin/studyservice/studyReading/")
public class AdminStudyReadingController {

    @Autowired
    StudyReadingService studyReadingService;

    //条件查询带分页的方法
    @PostMapping("pageReadingCondition/{current}/{limit}")
    public R pageReadingCondition(@PathVariable("current") long current,
                                    @PathVariable("limit") long limit,
                                    @RequestBody(required = false) BookQuery bookQuery) {

        Page<StudyReading> readingPage = new Page<>(current, limit);
        QueryWrapper<StudyReading> readingQueryWrapper = new QueryWrapper<>();

        String name = bookQuery.getName();
        String author = bookQuery.getAuthor();
        if (!StringUtils.isEmpty(name)) {
            readingQueryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(author)) {
            readingQueryWrapper.like("author", author);
        }
        studyReadingService.page(readingPage, readingQueryWrapper);
        long total = readingPage.getTotal();
        List<StudyReading> records = readingPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("deleteReadingById/{id}")
    public R deleteReadingById(@PathVariable("id") String id) {
        boolean removeById = studyReadingService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("getReadingById/{id}")
    public R getReadingById(@PathVariable("id") String id) {
        StudyReading reading = studyReadingService.getById(id);
        return R.ok().data("reading", reading);
    }

    @PostMapping("addReading")
    public R addReading(@RequestBody StudyReading studyReading) {
        boolean save = studyReadingService.save(studyReading);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("updateReading")
    public R updateReading(@RequestBody StudyReading studyReading) {
        boolean updateById = studyReadingService.updateById(studyReading);
        if (updateById) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addReadingByExcel")
    public R upload(MultipartFile file) {
        studyReadingService.importStudyReadingData(file, studyReadingService);
        return R.ok();
    }

}
